﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;
using Zwms.Client.Helper;

namespace Zwms.Client.ManageWms
{
    public partial class TransferBillQueryWnd : UserControl
    {
        #region 成员变量
        private BilTransferBill _selectedRecord;
        private PagerIndex _pageIndex;
        public BilTransferBill SelectedRecord { get { return _selectedRecord; } }
        #endregion

        #region 构造函数
        public TransferBillQueryWnd()
        {
            InitializeComponent();
            if (MainForm.CurrentUser.UserRole != "01")
            {
                this.tbsWarehouse.Tag = MainForm.CurrentUser.WarehouseCode;
                this.tbsWarehouse.SetTextBoxContent(MainForm.CurrentUser.WarehouseName);
                this.tbsWarehouse.SetEditState(false);
            }
            this._pageIndex = new PagerIndex(1, CommonDefine.iPageSize);
            this.InitBtnImage();
            this.ResetDataPager();
            BilTransferBill queryRecord = new BilTransferBill();
            this.LoadTsfBillList(queryRecord, ref _pageIndex);
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnQuery.Image = global::Zwms.Client.Properties.Resources.query;
            this.btnSelect.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.cancel;
        }

        private void ResetDataPager()
        {
            dataPager.PageCurrent = 1;
            this.dataPager.PageCurrent = CommonDefine.iPageSize;
        }

        private BilTransferBill GetUCRecord()
        {
            BilTransferBill queryRecord = new BilTransferBill();
            queryRecord.Id = this.tbTsfCode.Text.Trim();
            if (!string.IsNullOrWhiteSpace(this.tbsWarehouse.GetTextBoxContent()))
            {
                queryRecord.WarehouseCode = this.tbsWarehouse.Tag.ToString();
            }
            if (this.cbTsfType.SelectedIndex != -1)
            {
                queryRecord.TsfType = CommonHelper.GetDictKeyByValue(CommonDefine.DictTsfType, cbTsfType.SelectedItem.ToString());
            }
            if (this.dpuPlanDate.SelectDateTime != null)
            {
                queryRecord.PlanTsfDate = this.dpuPlanDate.SelectDateTime;
            }
            if (this.dpuTsfDate.SelectDateTime != null)
            {
                queryRecord.TsfDate = this.dpuTsfDate.SelectDateTime;
            }
            if (this.cbBillStatus.SelectedIndex != -1)
            {
                queryRecord.BillStatus = CommonHelper.GetDictKeyByValue(CommonDefine.DictTsfBillStatus, cbBillStatus.SelectedItem.ToString());
            }
            return queryRecord;
        }

        private void LoadTsfBillList(BilTransferBill record, ref PagerIndex pageIndex)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                BilTransferBill queryRecord = this.GetUCRecord();
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                IList<BilTransferBill> resultList = client.QueryTsfBillList(record, ref pageIndex);
                dataPager.RecordCount = pageIndex.RecordCount;
                this.bilTransferBillBindingSource.DataSource = null;
                this.bilTransferBillBindingSource.DataSource = resultList;
                this.Cursor = Cursors.Default;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
            
        }
        #endregion

        #region 控件函数
        private void btnQuery_Click(object sender, EventArgs e)
        {
            this.ResetDataPager();
            BilTransferBill queryRecord = this.GetUCRecord();
            this._pageIndex.CurrentPageIndex = 1;
            this.LoadTsfBillList(queryRecord, ref _pageIndex);
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (dgv.CurrentCell == null)
                return;
            int rowIndex = dgv.CurrentCell.RowIndex;
            if (rowIndex < 0)
            {
                MessageBox.Show("没有选中任何记录！");
                return;
            }
            DataGridViewRow dataRow = dgv.Rows[rowIndex];
            BilTransferBill record = new BilTransferBill();
            record.Id = dataRow.Cells[0].Value.ToString();
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            _selectedRecord = client.QueryTsfBill(record);
            if (this.Parent is Form)
            {
                Form window = this.Parent as Form;
                window.DialogResult = DialogResult.OK;
            }
            else
            {
                return;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (this.Parent is Form)
            {
                Form window = this.Parent as Form;
                window.DialogResult = DialogResult.Cancel;
                window.Close();
            }
            else
            {
                return;
            }
        }

        private void tbsWarehouse_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.WarehouseWnd uc = new InfoWms.WarehouseWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择仓库");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsWarehouse.SetTextBoxContent(uc.SelectedRecord.WarehouseName);
                tbsWarehouse.Tag = uc.SelectedRecord.Id;
            }
        }

        private void dataPager_BtnClick(object sender, EventArgs e)
        {
            _pageIndex.CurrentPageIndex = dataPager.PageCurrent;
            BilTransferBill queryRecord = this.GetUCRecord();
            this.LoadTsfBillList(queryRecord, ref _pageIndex);
            dataPager.RecordCount = _pageIndex.RecordCount;
        }
        #endregion
    }
}
